package com.xinghou.platform.server.api.service.impl;

import java.lang.reflect.ParameterizedType;

import org.springframework.dao.DataAccessException;

import com.caucho.hessian.client.HessianProxyFactory;
import com.xinghou.platform.util.ConfigUtil;

public class BaseDAOProtery<T> {	
	
	protected String httpServicesDaourl = ConfigUtil.getProperty("HTTP_SERVICE_DAO_URL");
	protected String httpServicesLuceneDaourl = ConfigUtil.getProperty("HTTP_SERVICE_DAO_LUCENE_URL");
	private HessianProxyFactory hessianFactory = new HessianProxyFactory();

	protected HessianProxyFactory getHessianFactory() {
		hessianFactory.setOverloadEnabled(true);
		return hessianFactory;
	}
	
	protected T getHessianDAO(String daoUrl)  throws DataAccessException{
		try {
			return (T)getTHessianDAO(daoUrl);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	protected T getTHessianDAO(String daoUrl)  throws Exception{
		Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		return (T)getHessianFactory().create(entityClass, daoUrl);
	}
	
}